import Vue from 'vue';
import Router from 'vue-router';
import envConfig from '../config/env.config.js';

// 防止重复this.$router.push()同一个路由时报错
const originalPush = Router.prototype.push;
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err);
};

Vue.use(Router);

const routerMap = [
  {
    name: 'index',
    path: '/',
    component: () => import('@/views/index'),
    meta: { title: '大屏管理页' }
  },
  {
    name: 'designer',
    path: '/screen-designer/:id',
    component: () => import('@/views/designer'),
    meta: { title: '大屏设计器' }
  },
  {
    name: 'views',
    path: '/screen-views/:id',
    component: () => import('@/views/designer/Preview'),
    meta: { title: '大屏数据展示' }
  }
];

const router = new Router({
  base: envConfig.baseRouter,
  mode: 'history',
  routes: routerMap
});

router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    document.title = to.meta.title;
  }
  next();
});

export default router;